package com.dididoctor.doctor.Activity.Patient;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import com.dididoctor.doctor.Activity.ConstantValue;
import com.dididoctor.doctor.MV.BaseFragment;
import com.dididoctor.doctor.R;
import com.dididoctor.doctor.Ui.customlistview.OnRefreshListener;
import com.dididoctor.doctor.Ui.customlistview.RefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 2.0
 * @类描述： 登陆页
 * @项目名称：ddgh
 * @类名称： loginActivity
 * @包名称： com.doudoukeji.service.Activity.login
 * @创建人：张强 boy
 * @创建时间：2016/3/7 16:40
 * @修改人：
 * @修改时间：2016/3/7 16:40
 * @修改备注： new parent class(用一句话描述修改了什么)
 */

@SuppressLint("ValidFragment")
public class PatlentModuleFragment extends BaseFragment implements View.OnClickListener, PatlentView {
    private Activity activity;
    private String type;
    private int pageIndex = 1;

    private RefreshListView listView;
    private List<Patient> patients = new ArrayList<Patient>();
    private PatlentPresenter forumPresenter;
    private PatientAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public PatlentModuleFragment(String type) {
        this.type = type;
    }

    @Override
    public void onAttach(Activity activity) {
        this.activity = activity;
        super.onAttach(activity);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_patient_list, null);
        this.initView(rootView);
        return rootView;

    }

    private void initView(View rootView) {


        listView = (RefreshListView) rootView.findViewById(R.id.lv_forum);
        listView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onPullDownRefresh() {
                pageIndex = 1;
                forumPresenter.getpatient(pageIndex, "", type);
            }

            @Override
            public void onLoadMoring() {
                pageIndex++;
                forumPresenter.getpatient(pageIndex, "", type);
            }

            @Override
            public void onScrollListener(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        listView.setHeadAndFoot(true, true);
        adapter = new PatientAdapter(getActivity(), patients,type);
        listView.setAdapter(adapter);

        forumPresenter = new PatlentPresenter(getActivity(), this);
        forumPresenter.getpatient(pageIndex, "", type);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void getpatientfail() {
        showToastMessage("网络连接失败，请检查您的网络设置");
        listView.onRefreshFinish();
    }

    @Override
    public void getpatientsucced(List<Patient> patients) {
        if (patients != null && patients.size() > 0) {
            if (pageIndex == 1) {
                this.patients.clear();
                adapter.setData(patients);
                listView.onRefreshFinish();
            }else{
                if (patients.size() < ConstantValue.LOAD_MESSAGE_COUNT) {
                    adapter.addData(patients);
                    listView.showNoMoreData();
                } else {
                    adapter.addData(patients);
                    listView.hideNoMoreData();
                }
            }
            adapter.notifyDataSetChanged();
        } else {
            if (pageIndex == 1) {
                listView.onRefreshFinish();
            }else{
                listView.showNoMoreData();
                adapter.notifyDataSetChanged();
            }
        }
    }
}
